#! /bin/bash
#
#  This file is part of the Metacircular Research Platform (MRP)
#
#      http://mrp.codehaus.org/
#
#  This file is licensed to you under the Eclipse Public License (EPL);
#  You may not use this file except in compliance with the License. You
#  may obtain a copy of the license at:
#
#      http://www.opensource.org/licenses/eclipse-1.0.php
#
#  See the COPYRIGHT.txt file distributed with this work for information
#  regarding copyright ownership.

export RVM_HOME="`dirname "$0"`"

export BASE_ARGS="-X:ic=$RVM_HOME/RVM.code.image -X:id=$RVM_HOME/RVM.data.image -X:ir=$RVM_HOME/RVM.rmap.image -X:vmClasses=$RVM_HOME/jksvm.jar:$RVM_HOME/rvmrt.jar:$RVM_HOME/lib/luni.jar:$RVM_HOME/lib/nio.jar:$RVM_HOME/lib/nio_char.jar:$RVM_HOME/lib/archive.jar:$RVM_HOME/lib/concurrent.jar:$RVM_HOME/lib/math.jar:$RVM_HOME/lib/regex.jar:$RVM_HOME/lib/icu4j-charsets-4_2_1.jar:$RVM_HOME/lib/icu4j-4_2_1.jar:$RVM_HOME/lib/yoko-rmi-impl.jar:$RVM_HOME/lib/instrument.jar:$RVM_HOME/lib/beans.jar:$RVM_HOME/lib/xml-apis.jar:$RVM_HOME/lib/mx4j.jar:$RVM_HOME/lib/xalan.jar:$RVM_HOME/lib/resolver.jar:$RVM_HOME/lib/logging.jar:$RVM_HOME/lib/bcprov.jar:$RVM_HOME/lib/security.jar:$RVM_HOME/lib/sql.jar:$RVM_HOME/lib/print.jar:$RVM_HOME/lib/mx4j-remote.jar:$RVM_HOME/lib/luni-kernel-stubs.jar:$RVM_HOME/lib/misc.jar:$RVM_HOME/lib/accessibility.jar:$RVM_HOME/lib/crypto.jar:$RVM_HOME/lib/yoko.jar:$RVM_HOME/lib/rmi.jar:$RVM_HOME/lib/security-kernel-stubs.jar:$RVM_HOME/lib/x-net.jar:$RVM_HOME/lib/imageio.jar:$RVM_HOME/lib/lang-management.jar:$RVM_HOME/lib/applet.jar:$RVM_HOME/lib/prefs.jar:$RVM_HOME/lib/annotation.jar:$RVM_HOME/lib/awt.jar:$RVM_HOME/lib/xercesImpl.jar:$RVM_HOME/lib/yoko-rmi-spec.jar:$RVM_HOME/lib/swing.jar:$RVM_HOME/lib/auth.jar:$RVM_HOME/lib/yoko-core.jar:$RVM_HOME/lib/text.jar:$RVM_HOME/lib/jndi.jar:$RVM_HOME/lib/suncompat.jar:$RVM_HOME/lib/sound.jar:$RVM_HOME/lib/bcel-5.2.jar -Duser.timezone=$(date +%Z) -Djava.home=$RVM_HOME -Djava.library.path=$RVM_HOME/lib -Dvm.boot.library.path=$RVM_HOME/lib -Duser.home=$HOME -Duser.dir=`pwd` -Duser.name=`whoami` -Dos.name=`uname -s` -Dos.version=`uname -r` -Dos.arch=`uname -m | tr ' ' '_'` -Dpath.separator=: -Dfile.separator=/ -Dfile.encoding=ISO-8859-1 -Djava.io.tmpdir=/tmp"

# This may need to be LIBPATH under AIX or SHLIB_PATH on HP-UX
export LD_LIBRARY_PATH=$RVM_HOME:$RVM_HOME/lib:$LD_LIBRARY_PATH

WRAP=exec

if ((( $# >= 2 )) && [[ $1 == -wrap ]] ); then
  WRAP=$2
  shift 2
fi

if (( $# == 0 )) || ((( $# >= 1 )) && [[ $1 != -gdb ]] ); then
  $WRAP "$RVM_HOME/JikesRVM" $BASE_ARGS "$@"
  exit
fi

# Remove -gdb arg
shift

export GDB_ARGS=
if (( $# >= 1 )) && [[ $1 == -fullname ]]; then
  GDB_ARGS="$1";
  shift
fi

export TMP_GDB_COMMANDS=$RVM_HOME/gdb.commandlineArgs

echo -n "set args" > $TMP_GDB_COMMANDS
echo $BASE_ARGS | tr ' ' '\n' | awk '{print "\"" $1 "\"" }' | tr '\n' ' ' >> $TMP_GDB_COMMANDS
#echo $* | tr ' ' '\n' | awk '{print "\"" $1 "\"" }' >> $TMP_GDB_COMMANDS
for arg in "$@"; do
    echo -n " \"$arg\"" >> $TMP_GDB_COMMANDS
done
echo "" >> $TMP_GDB_COMMANDS

# Tell gdb to completely ignore some signals that Jikes RVM uses
# for its own purposes.
# SIGSEGV is a null pointer exception
echo "handle SIGSEGV nostop noprint pass" >> $TMP_GDB_COMMANDS

# Include symbols if available
if [[ -e $RVM_HOME/JikesRVM-symbols.o ]]; then
  echo "add-symbol-file $RVM_HOME/JikesRVM-symbols.o 0x0" >> $TMP_GDB_COMMANDS
fi

# Set an initial breakpoint right before we start the jump into VM.boot
echo "break bootThread" >> $TMP_GDB_COMMANDS

gdb "${gdb_args[@]}" "$RVM_HOME/JikesRVM" "--command=$TMP_GDB_COMMANDS"
